﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace yunCloud
{
    public partial class MainForm : Form
    {
        double epsilon = 1e-2;
        bool pause = false;
        float angle = 0;
        Image image1 = null;
        ThreadStart threadStart = null;
        Thread thread = null;
        ThreadStart threadStart1 = null;
        Thread thread1 = null;
        List<String> musicPaths = new();
        List<String> Lyrics = new List<string>();
        List<int> beats = new List<int>();
        double pausePosition = 0.0;
        Stopwatch stopwatch = new Stopwatch();
        String[] messages = { "","","不敢回看", "左顾右盼不自然的暗自喜欢", "偷偷搭讪总没完地坐立难安", "试探说晚安多空泛又心酸",
        "低头呢喃","对你的偏爱太过于明目张胆","在原地打转的小丑伤心不断","空空留遗憾多难堪又为难","释然慵懒尽欢时间风干后你我再无关",
        "没答案怎么办看不惯自我欺瞒","纵容着喜欢的讨厌的宠溺的厌倦的","一个个慢慢黯淡","纵容着任性的随意的放肆的轻易的",
        "将所有欢脱倾翻","不应该太心软不大胆太死板不果断","玩弄着肆无忌惮","不应该舍弃了死心了放手了断念了","无可奈何不耐烦",
        "不算","灯火阑珊","我的心借了你的光是明是暗","笑自己情绪太泛滥形只影单","自嘲成习惯多敏感又难缠","低头呢喃","对你的偏爱太过于明目张胆",
        "在原地打转的小丑伤心不断","空空留遗憾多难堪又为难","释然慵懒尽欢时间风干后你与我再无关","没答案怎么办看不惯自我欺瞒","纵容着喜欢的讨厌的宠溺的厌倦的",
        "一个个慢慢黯淡","纵容着任性的随意的放肆的轻易的","将所有欢脱倾翻","不应该太心软不大胆太死板不果断","玩弄着肆无忌惮",
        "不应该舍弃了死心了放手了断念了","无可奈何不耐烦","任由着你躲闪我追赶你走散我呼喊","是谁在泛泛而谈","任由着你来了你笑了你走了不看我",
        "与理所当然分摊","不明白残存的没用的多余的不必的","破烂也在手紧攥","不明白谁赧然谁无端谁古板谁极端","无辜不知所以然",
        "纵容着喜欢的讨厌的宠溺的厌倦的（不管）","一个个慢慢黯淡","纵容着任性的随意的放肆的轻易的","将所有欢脱倾翻",
        "不应该太心软不大胆太死板不果断","玩弄着肆无忌惮","不应该舍弃了死心了放手了断念了","无可奈何不耐烦"};

        int[] interupt = { 0, 2, 7,11,18,20,24,29,34,43,51,57,60,65,69,75,78,83,
                            86,92,94,98,102,109,112,116,120,126,135,143,148,152,
                           157,160,166,169,175,196,201,204,210,213,219,222,227,
                            230,236,240,245,249,253, 256,262,};

        ManualResetEvent waitHandle = new ManualResetEvent(false);
        public MainForm()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            this.ControlBox = false;
            this.BackgroundImage = Image.FromFile(@"..\Image\背景.png");
            this.button1.BackgroundImage = Image.FromFile(@"..\Image\启动.png");
            this.button2.BackgroundImage = Image.FromFile(@"..\Image\下一首.png");
            this.button3.BackgroundImage = Image.FromFile(@"..\Image\上一首.png");
            this.button4.BackgroundImage = Image.FromFile(@"..\Image\退出.png");
            this.button5.BackgroundImage = Image.FromFile(@"..\Image\最小化.png");
            this.button6.BackgroundImage = Image.FromFile(@"..\Image\MV.png");
            this.pictureBox1.Image = Image.FromFile(@"..\Image\唱片1.png");
            this.pictureBox1.SendToBack();
            image1 = Image.FromFile(@"..\Image\陈粒.png");
            this.pictureBox2.Image = image1;

            Lyrics = messages.ToList();
            beats = interupt.ToList();

            readMusicFiles();

            this.axWindowsMediaPlayer1.Visible = false;

            //创建线程和委托
            threadStart = new ThreadStart(startRotate);
            thread = new Thread(threadStart);
            thread.IsBackground = true;

            //创建线程和委托
            threadStart1 = new ThreadStart(checkBeats);
            thread1 = new Thread(threadStart1);
            thread1.IsBackground = true;
        }

        public void readMusicFiles()
        {

            string folderPath = @"..\Music";

            // 确保路径以反斜杠结束
            if (!folderPath.EndsWith("\\"))
            {
                folderPath += "\\";
            }

            // 使用GetFiles方法获取文件夹下的所有文件
            List<string> files = new List<string>();
            string[] allFiles = Directory.GetFiles(folderPath);

            // 遍历所有文件，并将它们添加到列表中
            foreach (string file in allFiles)
            {
                musicPaths.Add(Path.GetFullPath(file));
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pause = !pause;
            if (pause)
            {
                this.button1.BackgroundImage = Image.FromFile(@"..\Image\暂停.png");
                this.pictureBox1.Image = Image.FromFile(@"..\Image\唱片2.png");
                this.axWindowsMediaPlayer1.URL = musicPaths[0];
                this.axWindowsMediaPlayer1.Ctlcontrols.currentPosition = pausePosition;
                this.axWindowsMediaPlayer1.Ctlcontrols.play();
                if (stopwatch.ElapsedMilliseconds == 0)
                {
                    stopwatch.Start();
                    thread1.Start();
                }
                else
                    stopwatch.Restart();               
                if (!thread.IsAlive)
                    thread.Start();
                else
                    waitHandle.Set();
            }
            else
            {
                this.button1.BackgroundImage = Image.FromFile(@"..\Image\启动.png");
                this.pictureBox1.Image = Image.FromFile(@"..\Image\唱片1.png");
                this.axWindowsMediaPlayer1.Ctlcontrols.pause();
                stopwatch.Stop();
                pausePosition = this.axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
            }
        }

        void checkBeats()
        {
            while(stopwatch != null)
            {
                for(int i = 0;i <beats.Count; i++)
                {
                    if(stopwatch.ElapsedMilliseconds / 1000 == beats[i])
                    {
                        int index = i;
                        label3.Text = Lyrics[index++];
                        label4.Text = Lyrics[index++];
                        label5.Text = Lyrics[index++];
                        label6.Text = Lyrics[index++];
                        label7.Text = Lyrics[index++];
                        label8.Text = Lyrics[index++];
                        label9.Text = Lyrics[index++];
                        label10.Text = Lyrics[index++];
                        label11.Text = Lyrics[index];
                    }
                }
            }
        }

        public Bitmap RotateImage(Image image, float angle)
        {
            if (image == null)
                throw new ArgumentNullException("image");

            float dx = image.Width / 2.0f;
            float dy = image.Height / 2.0f;
            
            Bitmap rotatedBmp = new Bitmap(image.Width, image.Height);
            rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
            Graphics g = Graphics.FromImage(rotatedBmp);
            g.TranslateTransform(dx, dy);
            g.RotateTransform(angle);
            g.TranslateTransform(-dx, -dy);
            g.DrawImage(image, new PointF(0, 0));
            return rotatedBmp;
        }
        void checkLyricItem()
        {

        }
        void refreshLyric(int index)
        {
            label3.Text = Lyrics[index++];
            label4.Text = Lyrics[index++];
            label5.Text = Lyrics[index++];
            label6.Text = Lyrics[index++];
            label7.Text = Lyrics[index++];
            label8.Text = Lyrics[index++];
            label9.Text = Lyrics[index++];
            label10.Text = Lyrics[index++];
            label11.Text = Lyrics[index];
        }

        void startRotate()
        {
            while (true)
            {
                if (!pause)
                {
                    waitHandle.WaitOne();
                    continue;
                }
                angle += 0.1f;
                if (angle >= 359) angle = 0;
                RotateImage(this.pictureBox2, image1, angle);
                Thread.Sleep(10);
            }
        }

        private void RotateImage(PictureBox pb, Image img, float angle)
        {
            if (img == null || pb.Image == null)
                return;
            pb.Image = RotateImage(img, angle);
        }


        private void MainForm_Load(object sender, EventArgs e)
        {
            //绘制圆形picturebox
            GraphicsPath gp = new GraphicsPath();
            gp.AddEllipse(this.pictureBox2.ClientRectangle);
            Region region = new Region(gp);
            this.pictureBox2.Region = region;
            gp.Dispose();
            region.Dispose();
            //取消跨线程的资源检查
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            return;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            return;
        }
    }
}
